<?php

namespace app\api\service;

use app\api\model\ProductAttrFieldModel;
use app\api\model\ProductModel;
use think\Db;
use think\Exception;
use think\Request;
use think\Validate;

class ProductAttrFieldService extends Api
{
    protected static $instance;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
    }

    public static function getInstance()
    {
        if (is_null(self::$instance))
        {
            self::$instance = new static();
        }
        return self::$instance;
    }

    /**
     * 添加编辑产品特殊属性字段
     */
    public function changeProductAttrValue($params)
    {
        $Validate = new Validate([
            'attr_id' => 'require',
            'title' => 'require',
            'cate' => 'require',
            'status' => 'require',
        ],[
            'attr_id' => '缺少属性id',
            'title' => '字段名称必填',
            'cate' => '字段类别必填',
            'status' => '字段状态必填',
        ]);
        if (!$Validate->check($params))
        {
            $this->error($Validate->getError());
        }
        if (in_array($params['cate'], ['select', 'selectxt']) && empty($params['value']))
        {
            $this->error('请维护下拉框可选值');
        }

        Db::startTrans();
        try{
            $saveData = [
                'attr_id' => $params['attr_id'],
                'title' => $params['title'],
                'cate' => $params['cate'],
//                'value' => isset($params['value']) ? $params['value'] : '',
                'status' => $params['status'],
//                'desc' => isset($params['desc']) ? $params['desc'] : ''
            ];
            if (!empty($params['value']))
            {
                $saveData['value'] = $params['value'];
            }
            if (isset($params['desc']))
            {
                $saveData['desc'] = $params['desc'];
            }
            if (!empty($params['id']))
            {
                $saveData['u_time'] = datetime(time(), 'Y-m-d H:i:s');
                $res = ProductAttrFieldModel::getInstance()->update($params['id'], $saveData);
            }else{
                $saveData['c_time'] = datetime(time(), 'Y-m-d H:i:s');
                $res = ProductAttrFieldModel::getInstance()->insert($saveData);
            }
            Db::commit();
            return $res;
        }catch (Exception $e)
        {
            Db::rollback();
            $this->error($e->getMessage());
        }
    }

    /**
     * 获取产品特殊属性字段列表
     */
    public function getProductAttrFieldList($params)
    {
        $Validate = new Validate([
            'attr_id' => 'require',
        ], [
            'attr_id' => '缺少属性id'
        ]);
        if (!$Validate->check($params))
        {
            $this->error($Validate->getError());
        }
        $page = $this->request->param('page', 1);
        $limit = $this->request->param('limit', 15);
        $start = ($page - 1) * $limit;

        $where['attr_id'] = ['eq', $params['attr_id']];

        if (!empty($params['title']))
        {
            $where['title'] = ['LIKE', '%'.$params['title'].'%'];
        }
        if (isset($params['status']))
        {
            $where['status'] = ['eq', $params['status']];
        }

        $total = ProductAttrFieldModel::getInstance()->count($where);
        $list = ProductAttrFieldModel::getInstance()->select($where, '*', $start, $limit);
        return [
            'total' => $total,
            'list' => $list
        ];
    }

    /**
     * 获取产品特殊属性字段信息
     */
    public function getProductAttrFieldInfo($params)
    {
        $Validate = new Validate([
            'id' => 'require'
        ]);
        if (!$Validate->check($params))
        {
            $this->error($Validate->getError());
        }
        $info = ProductAttrFieldModel::getInstance()->find($params['id']);
        return $info;
    }

    /**
     * 获取产品列表 - 下拉框使用
     */
    public function getSelectProductList()
    {
        $list = ProductModel::getInstance()->selectProduct([], 'id, product_name', 0, 100000);
        return $list;
    }
}